package edu.gduf.utils;


import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;

import edu.gduf.entity.Signin;

/**
 * excel工具类
 * Created by zhang on 2017/4/1.
 */
public class ExcelUtil {
    /**
     * @param filename 文件名
     * @param titles   单元格标题
     * @param content  内容
     * @param response 响应对象
     * @throws IOException
     */
	public static void exportExcel(String filename, String[] titles, List<Signin> content,
			HttpServletResponse response) {
        //创建excel文档对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //设置新的表单
        HSSFSheet sheet = workbook.createSheet();
        //设置第一行标题
        HSSFRow row1 = sheet.createRow(0);
        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row1.createCell(i);
            cell.setCellStyle(getTtileStyle(workbook));
            cell.setCellValue(titles[i]);
        }

		int index = 1;

		for (Signin signin : content) {
			HSSFRow row = sheet.createRow(index++);
			HSSFCell cell = row.createCell(0);
			cell.setCellStyle(getContentStyle(workbook));
			cell.setCellValue(signin.getClassId());
			HSSFCell cell2 = row.createCell(1);
			cell2.setCellStyle(getContentStyle(workbook));
			cell2.setCellValue(signin.getStuId());
			HSSFCell cell3 = row.createCell(2);
			cell3.setCellStyle(getContentStyle(workbook));
			cell3.setCellValue(signin.getStuName());
			HSSFCell cell4 = row.createCell(3);
			cell4.setCellStyle(getContentStyle(workbook));
			if (signin.getSigntime() == null) {
				cell4.setCellValue("");
			} else {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String format = simpleDateFormat.format(new Date(signin.getSigntime()));
				cell4.setCellValue(format);
			}
			HSSFCell cell5 = row.createCell(4);
			cell5.setCellStyle(getContentStyle(workbook));
			if (signin.getStatus() == null) {
				cell5.setCellValue("否");
			} else {
				cell5.setCellValue("是");
			}

		}

        //设置内容
		// Iterator iterator = content.iterator();
		// int index = 0;
		// while(iterator.hasNext()) {
		// index++;
		// HSSFRow row = sheet.createRow(index);
		// Signin signin = (Signin) iterator.next();
		// //根据反射获取属性
		// // Field[] fields = object.getClass().getDeclaredFields();
		// for (int i = 0; i < titles.length; i++) {
		// HSSFCell cell = row.createCell(i);
		// cell.setCellStyle(getContentStyle(workbook));
		// cell.setCellValue();

//                Field field = fields[i];
//                String fieldName = field.getName();
//                String getMethodName = "get"
//                        + fieldName.substring(0, 1).toUpperCase()
//                        + fieldName.substring(1);
//                try {
//                    Class tCls = object.getClass();
//                    Method getMethod = tCls.getMethod(getMethodName,
//                            new Class[] {});
//                    Object value = getMethod.invoke(object, new Object[] {});
//                    String textValue = null;
//
//                    if (value instanceof Boolean) {
//                        boolean bValue = (Boolean) value;
//                        textValue = "是";
//                        if (!bValue) {
//                            textValue ="否";
//                        }
//                    } else if (value instanceof Date) {
//                        String pattern = "yyyy-MM-dd HH:mm:ss";
//                        Date date = (Date) value;
//                        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
//                        textValue = sdf.format(date);
//                    } else{
//                        //其它数据类型都当作字符串简单处理
//                        textValue = value.toString();
//                    }
//                    cell.setCellValue(textValue);
//                } catch (NoSuchMethodException e) {
//                    e.printStackTrace();
//                } catch (IllegalAccessException e) {
//                    e.printStackTrace();
//                } catch (InvocationTargetException e) {
//                    e.printStackTrace();
//                }
//            }
		// }

        try {
            OutputStream outputStream = response.getOutputStream();
            /**
             * 设置response响应头
             */
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + new String(filename.getBytes(), "ISO8859-1")  +".xls");
            response.setContentType("application/msexcel;charset=utf-8");
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取标题样式
     * @return
     */
    private static HSSFCellStyle getTtileStyle(HSSFWorkbook workbook) {
        HSSFCellStyle style = workbook.createCellStyle();
        // 设置这些样式
        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 生成一个字体
        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.VIOLET.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        // 把字体应用到当前的样式
        style.setFont(font);
        return style;
    }
    /**
     * 获取内容样式
     */
    private static HSSFCellStyle getContentStyle(HSSFWorkbook workbook) {
        // 生成并设置另一个样式
        HSSFCellStyle style2 = workbook.createCellStyle();
        style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 生成另一个字体
        HSSFFont font2 = workbook.createFont();
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        // 把字体应用到当前的样式
        style2.setFont(font2);
        return style2;
    }

}
